package com.cqjtu.studentdocument.service.Impl;

import com.cqjtu.studentdocument.dao.CheckInfoDao;
import com.cqjtu.studentdocument.entity.CheckInfo;
import com.cqjtu.studentdocument.service.CheckInfoService;
import com.cqjtu.studentdocument.utils.vo.AnalysisChoose;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CheckInfoServiceImpl extends BaseServiceImpl<CheckInfo,CheckInfoDao> implements CheckInfoService {


    @Override
    public List<CheckInfo> getStudyYear() {
        List<CheckInfo> checkInfoList = entityDao.getStudyYear();
        return checkInfoList;
    }


    //整体数据分析
    @Override
    public Map<String,Object> analysisAll(String studyYear) {
        DecimalFormat df = new DecimalFormat("#.0");

        CheckInfo checkInfo = new CheckInfo();
        checkInfo.setStudyYear(studyYear);

        checkInfo.setCheckYear("第一学年");
        List<CheckInfo> checkInfoListFirst = entityDao.select(checkInfo);
        int boyNum1 = entityDao.checkCountBySex("男",studyYear,"第一学年");
        int girlNum1 = entityDao.checkCountBySex("女",studyYear,"第一学年");
        checkInfo.setCheckYear("第二学年");
        List<CheckInfo> checkInfoListSeconed = entityDao.select(checkInfo);
        int boyNum2 = entityDao.checkCountBySex("男",studyYear,"第二学年");
        int girlNum2 = entityDao.checkCountBySex("女",studyYear,"第二学年");
        checkInfo.setCheckYear("第三学年");
        List<CheckInfo> checkInfoListThird = entityDao.select(checkInfo);
        int boyNum3 = entityDao.checkCountBySex("男",studyYear,"第三学年");
        int girlNum3 = entityDao.checkCountBySex("女",studyYear,"第三学年");
        checkInfo.setCheckYear("第四学年");
        List<CheckInfo> checkInfoListFourth = entityDao.select(checkInfo);
        int boyNum4 = entityDao.checkCountBySex("男",studyYear,"第四学年");
        int girlNum4 = entityDao.checkCountBySex("女",studyYear,"第四学年");
        //BMI临时储存
        double allStudentBMI = 0.0;
        double allBoyBMI = 0.0;
        double allGirlBMI = 0.0;

        //身高临时储存
        double allHeight = 0.0;
        double allBoyHeight = 0.0;
        double allGirlHeight = 0.0;

        //体重临时储存
        double allWeight = 0.0;
        double allBoyWeight = 0.0;
        double allGirlWeight = 0.0;

        //BMI数据
        List<String> studentBMI = new ArrayList<>();
        List<String> boyBMI = new ArrayList<>();
        List<String> girlBMI = new ArrayList<>();

        //身高数据
        List<String> studentHeight = new ArrayList<>();
        List<String> boyHeight = new ArrayList<>();
        List<String> girlHeight = new ArrayList<>();

        //体重数据
        List<String> studentWeight = new ArrayList<>();
        List<String> boyWeight = new ArrayList<>();
        List<String> girlWeight = new ArrayList<>();

        //第一学年
        for(CheckInfo checkInfo1:checkInfoListFirst){
            double height = checkInfo1.getHeight();
            double weight = checkInfo1.getWeight();

            allHeight += height;
            allWeight += weight;
            double bmi =Double.valueOf(df.format(weight*10000/(height*height))) ;
            allStudentBMI+=bmi;
            if("男".equals(checkInfo1.getSex())){
                if(checkInfo1.getHeight()==null||checkInfo1.getWeight()==null){
                    boyNum1--;
                    continue;
                }
                allBoyBMI+=bmi;
                allBoyHeight+=height;
                allBoyWeight+=weight;
            }else{
                if(checkInfo1.getHeight()==null||checkInfo1.getWeight()==null){
                    girlNum1--;
                    continue;
                }
                allGirlBMI+=bmi;
                allGirlHeight+=height;
                allGirlWeight+=weight;
            }
        }
        int studentNum1 = boyNum1+girlNum1;
        studentBMI.add(df.format(allStudentBMI/studentNum1));
        boyBMI.add(df.format(allBoyBMI/boyNum1));
        girlBMI.add(df.format(allGirlBMI/girlNum1));
        studentHeight.add(df.format(allHeight/studentNum1));
        boyHeight.add(df.format(allBoyHeight/boyNum1));
        girlHeight.add(df.format(allGirlHeight/girlNum1));
        studentWeight.add(df.format(allWeight/studentNum1));
        boyWeight.add(df.format(allBoyWeight/boyNum1));
        girlWeight.add(df.format(allGirlWeight/girlNum1));

        allStudentBMI=allBoyBMI=allGirlBMI=allWeight=allBoyWeight=allGirlWeight=allHeight=allBoyHeight=allGirlHeight=0.0;

        //第二学年
        for(CheckInfo checkInfo1:checkInfoListSeconed){
            double height = checkInfo1.getHeight();
            double weight = checkInfo1.getWeight();
            allHeight += height;
            allWeight += weight;
            double bmi =Double.valueOf(df.format(weight*10000/(height*height))) ;
            allStudentBMI+=bmi;
            if("男".equals(checkInfo1.getSex())){
                if(checkInfo1.getHeight()==null||checkInfo1.getWeight()==null){
                    boyNum2--;
                    continue;
                }
                allBoyBMI+=bmi;
                allBoyHeight+=height;
                allBoyWeight+=weight;
            }else{
                if(checkInfo1.getHeight()==null||checkInfo1.getWeight()==null){
                    girlNum2--;
                    continue;
                }
                allGirlBMI+=bmi;
                allGirlHeight+=height;
                allGirlWeight+=weight;
            }
        }
        int studentNum2 = boyNum2+girlNum2;
        studentBMI.add(df.format(allStudentBMI/studentNum2));
        boyBMI.add(df.format(allBoyBMI/boyNum2));
        girlBMI.add(df.format(allGirlBMI/girlNum2));
        studentHeight.add(df.format(allHeight/studentNum2));
        boyHeight.add(df.format(allBoyHeight/boyNum2));
        girlHeight.add(df.format(allGirlHeight/girlNum2));
        studentWeight.add(df.format(allWeight/studentNum2));
        boyWeight.add(df.format(allBoyWeight/boyNum2));
        girlWeight.add(df.format(allGirlWeight/girlNum2));
        allStudentBMI=allBoyBMI=allGirlBMI=allWeight=allBoyWeight=allGirlWeight=allHeight=allBoyHeight=allGirlHeight=0.0;

        //第三学年
        for(CheckInfo checkInfo1:checkInfoListThird){
            double height = checkInfo1.getHeight();
            double weight = checkInfo1.getWeight();
            allHeight += height;
            allWeight += weight;
            double bmi =Double.valueOf(df.format(weight*10000/(height*height))) ;
            allStudentBMI+=bmi;
            if("男".equals(checkInfo1.getSex())){
                if(checkInfo1.getHeight()==null||checkInfo1.getWeight()==null){
                    boyNum3--;
                    continue;
                }
                allBoyBMI+=bmi;
                allBoyHeight+=height;
                allBoyWeight+=weight;
            }else{
                if(checkInfo1.getHeight()==null||checkInfo1.getWeight()==null){
                    girlNum3--;
                    continue;
                }
                allGirlBMI+=bmi;
                allGirlHeight+=height;
                allGirlWeight+=weight;
            }
        }
        int studentNum3 = boyNum3+girlNum3;
        studentBMI.add(df.format(allStudentBMI/studentNum3));
        boyBMI.add(df.format(allBoyBMI/boyNum3));
        girlBMI.add(df.format(allGirlBMI/girlNum3));
        studentHeight.add(df.format(allHeight/studentNum3));
        boyHeight.add(df.format(allBoyHeight/boyNum3));
        girlHeight.add(df.format(allGirlHeight/girlNum3));
        studentWeight.add(df.format(allWeight/studentNum3));
        boyWeight.add(df.format(allBoyWeight/boyNum3));
        girlWeight.add(df.format(allGirlWeight/girlNum3));
        allStudentBMI=allBoyBMI=allGirlBMI=allWeight=allBoyWeight=allGirlWeight=allHeight=allBoyHeight=allGirlHeight=0.0;

        //第四学年
        for(CheckInfo checkInfo1:checkInfoListFourth){
            double height = checkInfo1.getHeight();
            double weight = checkInfo1.getWeight();
            allHeight += height;
            allWeight += weight;
            double bmi =Double.valueOf(df.format(weight*10000/(height*height))) ;
            allStudentBMI+=bmi;
            if("男".equals(checkInfo1.getSex())){
                if(checkInfo1.getHeight()==null||checkInfo1.getWeight()==null){
                    boyNum4--;
                    continue;
                }
                allBoyBMI+=bmi;
                allBoyHeight+=height;
                allBoyWeight+=weight;
            }else{
                if(checkInfo1.getHeight()==null||checkInfo1.getWeight()==null){
                    girlNum4--;
                    continue;
                }
                allGirlBMI+=bmi;
                allGirlHeight+=height;
                allGirlWeight+=weight;
            }
        }
        int studentNum4 = boyNum4+girlNum4;
        studentBMI.add(df.format(allStudentBMI/studentNum4));
        boyBMI.add(df.format(allBoyBMI/boyNum4));
        girlBMI.add(df.format(allGirlBMI/girlNum4));
        studentHeight.add(df.format(allHeight/studentNum4));
        boyHeight.add(df.format(allBoyHeight/boyNum4));
        girlHeight.add(df.format(allGirlHeight/girlNum4));
        studentWeight.add(df.format(allWeight/studentNum4));
        boyWeight.add(df.format(allBoyWeight/boyNum4));
        girlWeight.add(df.format(allGirlWeight/girlNum4));
        allStudentBMI=allBoyBMI=allGirlBMI=allWeight=allBoyWeight=allGirlWeight=allHeight=allBoyHeight=allGirlHeight=0.0;

        Map<String,Object> result = new HashMap<>();
        result.put("allBMI",studentBMI);
        result.put("boyBMI",boyBMI);
        result.put("girlBMI",girlBMI);
        result.put("allHeight",studentHeight);
        result.put("boyHeight",boyHeight);
        result.put("girlHeight",girlHeight);
        result.put("allWeight",studentWeight);
        result.put("boyWeight",boyWeight);
        result.put("girlWeight",girlWeight);

        return result;
    }

    @Override
    public Map<String, Object> analysis(AnalysisChoose analysisChoose) {
        CheckInfo checkInfo = new CheckInfo();
        String college = analysisChoose.getCollege();
        String checkYear = analysisChoose.getCheckYear();
        String studyYear = analysisChoose.getStudyYear();
        if(checkYear!=""){
            checkInfo.setCheckYear(analysisChoose.getCheckYear());
        }
        if(studyYear!=""){
            checkInfo.setStudyYear(analysisChoose.getStudyYear());
        }
        if(college!=""){
            checkInfo.setCollege(analysisChoose.getCollege());
        }
        List<CheckInfo> checkInfoList = entityDao.select(checkInfo);

        Map<String,Object> result = new HashMap<>();
        result.putAll(bodyFatRate(checkInfoList));
        result.putAll(view(checkInfoList));
        result.put("fatRate",fatNum(checkInfoList));
        result.putAll(heightNum(checkInfoList));
        result.putAll(pulse(checkInfoList));
        result.putAll(blood(checkInfoList));
        return result;
    }

    //具体数据分析
    private Map<String,Object> bodyFatRate(List<CheckInfo> checkInfoList){
        int [] allNum = new int[7];
        int [] boyNum = new int[7];
        int [] girlNum = new int[7];
        Map<String,Object> map = new HashMap<String,Object>();
        for(CheckInfo checkInfo:checkInfoList){
            double rate = 0.0;
            double weight = checkInfo.getWeight();
            double waistline = checkInfo.getWaistline();
            if("男".equals(checkInfo.getSex())){
                rate = waistline*0.74-weight*0.082-44.72;
                if(rate<=5){
                    boyNum[0]+=1;
                }else if(rate<=10){
                    boyNum[1]+=1;
                }else if(rate<=15){
                    boyNum[2]+=1;
                }else if(rate<=20) {
                    boyNum[3]+=1;
                }else if(rate<=25){
                    boyNum[4]+=1;
                }else if(rate<=30){
                    boyNum[5]+=1;
                }else{
                    boyNum[6]+=1;
                }
            }else{
                rate = waistline*0.74-weight*0.082-34.89;
                if(rate<=5){
                    girlNum[0]+=1;
                }else if(rate<=10){
                    girlNum[1]+=1;
                }else if(rate<=15){
                    girlNum[2]+=1;
                }else if(rate<=20) {
                    girlNum[3]+=1;
                }else if(rate<=25){
                    girlNum[4]+=1;
                }else if(rate<=30){
                    girlNum[5]+=1;
                }else{
                    girlNum[6]+=1;
                }
            }
        }
        for(int i=0;i<allNum.length;i++){
            allNum[i]=boyNum[i]+girlNum[i];
        }
        map.put("BF",allNum);
        map.put("BFboy",boyNum);
        map.put("BFgirl",girlNum);
        return map;
    }

    private Map<String,Object> view(List<CheckInfo> checkInfoList){
        int [] allNum = new int[6];
        int [] boyNum = new int[6];
        int [] girlNum = new int[6];
        Map<String,Object> map = new HashMap<String,Object>();
        for(CheckInfo checkInfo:checkInfoList){
            double right = 0.0;
            double left = 0.0;
            double aver = 0.0;
            if(checkInfo.getRightView()!=null){
                right = checkInfo.getRightView();
            }
            if(checkInfo.getLeftView()!=null){
                left = checkInfo.getLeftView();
            }
            if(right==0.0&&left==0.0){
                continue;
            }else if(right==0.0){
                aver = left;
            }else if(left==0.0){
                aver = right;
            }else{
                aver = (right+left)/2;
            }
            if(aver<=3.0){
                if("男".equals(checkInfo.getSex())){
                    boyNum[0]+=1;
                }else{
                    girlNum[0]+=1;
                }
            }else if(aver<=3.5){
                if("男".equals(checkInfo.getSex())){
                    boyNum[1]+=1;
                }else{
                    girlNum[1]+=1;
                }
            }
            else if(aver<=4.0){
                if("男".equals(checkInfo.getSex())){
                    boyNum[2]+=1;
                }else{
                    girlNum[2]+=1;
                }
            }
            else if(aver<=4.5){
                if("男".equals(checkInfo.getSex())){
                    boyNum[3]+=1;
                }else{
                    girlNum[3]+=1;
                }
            }
            else if(aver<=5.0){
                if("男".equals(checkInfo.getSex())){
                    boyNum[4]+=1;
                }else{
                    girlNum[4]+=1;
                }
            }else{
                if("男".equals(checkInfo.getSex())){
                    boyNum[5]+=1;
                }else{
                    girlNum[5]+=1;
                }
            }
        }
        for (int i=0;i<allNum.length;i++){
            allNum[i]= boyNum[i]+girlNum[i];
        }
        map.put("allView",allNum);
        map.put("boyView",boyNum);
        map.put("girlView",girlNum);
        return map;
    }

    private List<Map<String,Object>> fatNum(List<CheckInfo> checkInfoList){
        int [] fatNum = new int[4];
        for(CheckInfo checkInfo:checkInfoList){
            if(checkInfo.getWeight()==null||checkInfo.getHeight()==null){
                continue;
            }
            double height = checkInfo.getHeight();
            double weight = checkInfo.getWeight();
            double bmi = weight*10000/(height*height);
            if(bmi<=18.4){
                fatNum[0]+=1;
            }else if(bmi<=23.9){
                fatNum[1]+=1;
            }else if(bmi<=27.9){
                fatNum[2]+=1;
            }else{
                fatNum[3]+=1;
            }
        }
        List<Map<String,Object>> list = new ArrayList<>();
        for(int i=0;i<fatNum.length;i++){
            Map<String,Object> map = new HashMap<>();
            map.put("value",fatNum[i]);
            switch (i){
                case 0:
                    map.put("name","偏瘦");
                    break;
                case 1:
                    map.put("name","正常");
                    break;
                case 2:
                    map.put("name","过重");
                    break;
                case 3:
                    map.put("name","肥胖");
                    break;
                default:break;
            }
            list.add(map);
        }
        return list;
    }

    private Map<String,Object> heightNum(List<CheckInfo> checkInfoList){
        int [] allNum = new int[6];
        int [] boyNum = new int[6];
        int [] girlNum = new int[6];
        Map<String,Object> map = new HashMap<String,Object>();
        for(CheckInfo checkInfo:checkInfoList){
            if(checkInfo.getHeight()==null){
                continue;
            }
            double height = checkInfo.getHeight();
            if(height<=150){
                if("男".equals(checkInfo.getSex())){
                    boyNum[0]+=1;
                }else{
                    girlNum[0]+=1;
                }
            }else if(height<=160){
                if("男".equals(checkInfo.getSex())){
                    boyNum[1]+=1;
                }else{
                    girlNum[1]+=1;
                }
            }else if(height<=170){
                if("男".equals(checkInfo.getSex())){
                    boyNum[2]+=1;
                }else{
                    girlNum[2]+=1;
                }
            }else if(height<=180){
                if("男".equals(checkInfo.getSex())){
                    boyNum[3]+=1;
                }else{
                    girlNum[3]+=1;
                }
            }else if(height<=190){
                if("男".equals(checkInfo.getSex())){
                    boyNum[4]+=1;
                }else{
                    girlNum[4]+=1;
                }
            }else {
                if("男".equals(checkInfo.getSex())){
                    boyNum[5]+=1;
                }else{
                    girlNum[5]+=1;
                }
            }
        }
        for (int i=0;i<allNum.length;i++){
            allNum[i]=boyNum[i]+girlNum[i];
        }
        map.put("allHeight",allNum);
        map.put("boyHeight",boyNum);
        map.put("girlHeight",girlNum);
        return map;
    }

    private Map<String,Object> pulse(List<CheckInfo> checkInfoList){
        int [] allNum = new int[7];
        int [] boyNum = new int[7];
        int [] girlNum = new int[7];
        Map<String,Object> map = new HashMap<String,Object>();
        for(CheckInfo checkInfo:checkInfoList){
            if(checkInfo.getPulse()==null){
                continue;
            }
            double pulse = checkInfo.getPulse();
            if(pulse<=60){
                if("男".equals(checkInfo.getSex())){
                    boyNum[0]+=1;
                }else{
                    girlNum[0]+=1;
                }
            }else if(pulse<=70){
                if("男".equals(checkInfo.getSex())){
                    boyNum[1]+=1;
                }else{
                    girlNum[1]+=1;
                }
            }else if(pulse<=80){
                if("男".equals(checkInfo.getSex())){
                    boyNum[2]+=1;
                }else{
                    girlNum[2]+=1;
                }
            }else if(pulse<=90){
                if("男".equals(checkInfo.getSex())){
                    boyNum[3]+=1;
                }else{
                    girlNum[3]+=1;
                }
            }else if(pulse<=100){
                if("男".equals(checkInfo.getSex())){
                    boyNum[4]+=1;
                }else{
                    girlNum[4]+=1;
                }
            }else if(pulse<=110){
                if("男".equals(checkInfo.getSex())){
                    boyNum[5]+=1;
                }else{
                    girlNum[5]+=1;
                }
            }else {
                if("男".equals(checkInfo.getSex())){
                    boyNum[6]+=1;
                }else{
                    girlNum[6]+=1;
                }
            }
        }
        for (int i=0;i<allNum.length;i++){
            allNum[i]=boyNum[i]+girlNum[i];
        }
        map.put("allpulse",allNum);
        map.put("boypulse",boyNum);
        map.put("girlpulse",girlNum);
        return map;
    }

    private Map<String,Object> blood(List<CheckInfo> checkInfoList){
        int [] allNum = new int[6];
        int [] boyNum = new int[6];
        int [] girlNum = new int[6];
        Map<String,Object> map = new HashMap<String,Object>();
        for(CheckInfo checkInfo:checkInfoList){
            if(checkInfo.getBloodPressure()==null){
                continue;
            }
            double blood = checkInfo.getBloodPressure();
            if(blood<=60){
                if("男".equals(checkInfo.getSex())){
                    boyNum[0]+=1;
                }else{
                    girlNum[0]+=1;
                }
            }else if(blood<=80){
                if("男".equals(checkInfo.getSex())){
                    boyNum[1]+=1;
                }else{
                    girlNum[1]+=1;
                }
            }else if(blood<=100){
                if("男".equals(checkInfo.getSex())){
                    boyNum[2]+=1;
                }else{
                    girlNum[2]+=1;
                }
            }else if(blood<=120){
                if("男".equals(checkInfo.getSex())){
                    boyNum[3]+=1;
                }else{
                    girlNum[3]+=1;
                }
            }else if(blood<=140){
                if("男".equals(checkInfo.getSex())){
                    boyNum[4]+=1;
                }else{
                    girlNum[4]+=1;
                }
            }else {
                if("男".equals(checkInfo.getSex())){
                    boyNum[5]+=1;
                }else{
                    girlNum[5]+=1;
                }
            }
        }
        for (int i=0;i<allNum.length;i++){
            allNum[i]=boyNum[i]+girlNum[i];
        }
        map.put("allBlood",allNum);
        map.put("boyBlood",boyNum);
        map.put("girlBlood",girlNum);
        return map;
    }


}
